package com.gcxy.utils;

import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Author qinlun
 * @Date 2024/4/10 10:29
 * @Describe
 **/
public class DateUtil {

    /**
     * 获取当前时间对应的周时间区间
     *
     * @return
     */
    public static String getWeekTime() {
        LocalDate now = LocalDate.now();
        LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return startOfWeek.toString() + "~" + endOfWeek.toString();
    }

    /**
     * date类型转localDate
     *
     * @param time
     * @return
     */
    public static LocalDate dateToLocalDate(Date time) {
        LocalDateTime localDateTime = dateToLocalDateTime(time);
        // 从 LocalDateTime 中提取 LocalDate
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

    /**
     * date类型转localDateTime
     *
     * @param time
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date time) {
        // 将 java.util.Date 转换为 java.time.Instant
        Instant instant = time.toInstant();
        // 获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
        // 使用 Instant 和时区转换为 LocalDateTime
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }
}

